package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author BaiZY on 2020/8/25.
 * @version 1.0
 */
@RequestMapping("/goods")
@RestController
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SpuDetailService spuDetailService;

    @Autowired
    private SkuService skuService;

    /**
     *分页查询spu
     * @param bid
     * @param cid
     * @param id
     * @param page
     * @param rows
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "brandId",required = false)Long bid ,
            @RequestParam(value = "categoryId",required = false)Long cid ,
            @RequestParam(value = "id",required = false)Long id ,
            @RequestParam(value = "page",defaultValue = "1")Long page ,
            @RequestParam(value = "rows",defaultValue = "5")Long rows ,
            @RequestParam(value = "saleable",required = false)Boolean saleable){

            PageDTO<SpuDTO> pageDTO = spuService.queryByPage(bid,cid,id,page,rows,saleable);
            return ResponseEntity.ok(pageDTO);
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(new SpuDTO(spuService.getById(id)));
    }

    /**
     *根据id查询商品详情
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id")Long id){
        return ResponseEntity.ok(new SpuDetailDTO(spuDetailService.getById(id)));
    }

    /**
     * 根据id批量查询sku
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.listByIds(ids)));
    }

    /**
     * 根据spuID查询sku的集合
     * @param spuId
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id")Long spuId){
        List<SkuDTO> list = skuService.querySkuBySpuId(spuId);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据id查询spu及sku、spuDetail等
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){
        SpuDTO spuDTO = spuService.queryGoodsById(id);
        return ResponseEntity.ok(spuDTO);
    }

    /**
     *新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品上下架
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam(value = "id",required = false)Long id,
                                                  @RequestParam(value = "saleable",required = false)Boolean saleable){

            spuService.updateSpuSaleable(id,saleable);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 更新商品
     * @param spuDTO
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     *根据spuId查询spu的所有规格参数值
     * @param spuId
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecValues(
            @RequestParam("id") Long spuId,
            @RequestParam(value = "searching",required = false)Boolean searching){

        return ResponseEntity.ok(spuDetailService.querySpecValues(spuId,searching));
    }

    /**
     * 扣减库存
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }

    /**
     * 扣减库存
     */
    @PutMapping("/stock/plus")
    public ResponseEntity<Void> plusStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.plusStock(cartMap);
        return ResponseEntity.noContent().build();
    }
}
